﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using DG.Tweening;
public class LevelPixelLove : MonoBehaviour
{
    public List<Button> buttonList = new List<Button>();
    private List<int> indexList = new List<int>();
    private List<int> defaultIndexList = new List<int>();
    public GameLevelItem gameLevelItem;
    // Start is called before the first frame update
    void Start()
    {
        InitIndexList();
        InitDefaultIndex();
        InitButtonEvent();
    }

    private void InitIndexList()
    {
        indexList.Clear();
        //0
        indexList.Add(0);
        //3
        indexList.Add(3);
        //6
        indexList.Add(6);
        //9
        indexList.Add(9);
        //12
        indexList.Add(12);
        //13
        indexList.Add(13);
        //14
        indexList.Add(14);
        //16
        indexList.Add(16);
        //18
        indexList.Add(18);
        //20
        indexList.Add(20);
        //21
        indexList.Add(21);
        //23
        indexList.Add(23);
        //24
        indexList.Add(24);
        //26
        indexList.Add(26);
        //28
        indexList.Add(28);
        //30
        indexList.Add(30);
        //32
        indexList.Add(32);
        //33
        indexList.Add(33);
        //35
        indexList.Add(35);
        //36
        indexList.Add(36);
        //38
        indexList.Add(38);
        //39
        indexList.Add(39);
        //41
        indexList.Add(41);
        //43
        indexList.Add(43);
        //45
        indexList.Add(45);
        //46
        indexList.Add(46);
        //47
        indexList.Add(47);
        //48
        indexList.Add(48);
        //51
        indexList.Add(51);
        //52
        indexList.Add(52);
        //53
        indexList.Add(53);
        //54
        indexList.Add(54);
        //57
        indexList.Add(57);
        //58
        indexList.Add(58);
        //59
        indexList.Add(59);
    }

    private void InitDefaultIndex()
    {
        defaultIndexList.Clear();

        //0
        defaultIndexList.Add(0);
        //3
        defaultIndexList.Add(3);
        //6
        defaultIndexList.Add(6);
        //16
        defaultIndexList.Add(16);
        //18
        defaultIndexList.Add(18);
        //20
        defaultIndexList.Add(20);
        //23
        defaultIndexList.Add(23);
        //26
        defaultIndexList.Add(26);
        //30
        defaultIndexList.Add(30);
        //32
        defaultIndexList.Add(32);
        //33
        defaultIndexList.Add(33);
        //43
        defaultIndexList.Add(43);
        //45
        defaultIndexList.Add(45);
        //46
        defaultIndexList.Add(46);
        //47
        defaultIndexList.Add(47);
        //48
        defaultIndexList.Add(48);
        //52
        defaultIndexList.Add(52);
        //53
        defaultIndexList.Add(53);
    }

    private void InitButtonEvent()
    {
        int count = buttonList.Count;
        for (int i = 0; i < count; i++)
        {
            int index = i;
            if (defaultIndexList.Contains(i))
                buttonList[index].image.color = Color.black;
            buttonList[i].onClick.AddListener(() =>
            {
                SoundManager.Instance.PlayAudio(SoundType.Click.ToString(), false);
                if (buttonList[index].image.color == Color.white)
                    buttonList[index].image.color = Color.black;
                else
                    buttonList[index].image.color = Color.white;

                JudgeResult();
            });
        }
    }

    private void DebugBlackBrickIndex()
    {
        int count = buttonList.Count;
        string blackIndexStr = string.Empty;
        for (int i = 0; i < count; i++)
        {
            if (buttonList[i].image.color == Color.black)
                blackIndexStr += i.ToString() + "\n";
        }

        Debug.Log(blackIndexStr);
    }

    private void Update()
    {
        if (Input.GetKeyDown(KeyCode.A))
        {
            DebugBlackBrickIndex();
        }
    }

    private void JudgeResult()
    {
        int count = buttonList.Count;
        bool result = true;
        int rightCount = 0;
        for (int i = 0; i < count; i++)
        {
            if (buttonList[i].image.color == Color.black)
            {
                if (!indexList.Contains(i))
                {
                    result = false;
                    break;
                }
                rightCount++;
            }
        }
        if (rightCount != indexList.Count)
            result = false;

        if (result)
        {
            HideOtherButton();
            gameLevelItem.TriggerResult(true);
        }
    }

    private void HideOtherButton()
    {
        int count = buttonList.Count;
        for (int i = 0; i < count; i++)
        {
            buttonList[i].enabled = false;
            if (!indexList.Contains(i))
                buttonList[i].image.DOFade(0, 0.5f);
        }
    }
}
